const { default: request } = require("../../utils/request");
const app = getApp();
// pages/poster/poster.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showPrivacy: true,
    fileList: [],
    showpage: false,
    pageList: ["首页", "产品", "配置清单", "我的"],
    name: "",
    page: "",
    size: "",
    imageList: [],
    buttonShow: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getImage();
  },
  handleAgreePrivacyAuthorization() {
    // 用户点击同意按钮后
    this.resolvePrivacyAuthorization({ buttonId: "agree-btn", event: "agree" });
  },

  afterRead(event) {
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: "https://example.weixin.qq.com/upload", // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: "file",
      formData: { user: "test" },
      success(res) {
        // 上传完成需要更新 fileList
        const { fileList = [] } = this.data;
        fileList.push({ ...file, url: res.data });
        this.setData({ fileList });
      },
    });
  },

  goback() {
    wx.navigateBack({
      delta: 1,
    });
  },
  onpage() {
    this.setData({
      showpage: true,
    });
  },
  onCancelPage() {
    this.setData({
      showpage: false,
    });
  },
  onConfirmPage(e) {
    if (e.detail.value == "首页") {
      this.setData({
        page: "pages/index/index",
      });
    } else if (e.detail.value == "产品") {
      this.setData({
        page: "pages/category/category",
      });
    } else if (e.detail.value == "配置清单") {
      this.setData({
        page: "pages/order/order",
      });
    } else if (e.detail.value == "我的") {
      this.setData({
        page: "pages/home/home",
      });
    }
    this.setData({
      showpage: false,
    });
  },
  onName(e) {
    this.setData({
      name: e.detail.value,
    });
  },
  onSize(e) {
    this.setData({
      size: e.detail.value,
    });
  },
  async submit() {
    const name = wx.getStorageSync("username");
    const res = await request({
      url:
        app.globalData.baseURL + "/app-user-api/member/qrcode/miniapp/create",
      method: "POST",
      data: {
        name: name,
        path: pages / index / index,
        px: 430,
        version: "release",
      },
    });
    if (res.code == 0) {
      wx.showToast({
        title: "生成二维码成功",
        icon: "success",
      });
      this.setData({
        name: "",
        page: "",
        size: "",
      });
      this.getImage();
    } else {
      wx.showToast({
        title: res.msg,
        icon: "none",
      });
    }
  },
  clear() {
    this.setData({
      name: "",
      page: "",
      size: "",
    });
  },
  async getImage() {
    const userId = wx.getStorageSync("userId");
    const res = await request({
      url: app.globalData.baseURL + "/app-user-api/member/qrcode/miniapp/page",
      method: "GET",
      data: {
        pageNo: 1,
        pageSize: 50,
        memberId: userId,
      },
    });
    if (res.data.total === 0) {
      this.setData({
        buttonShow: false,
      });
    } else {
      this.setData({
        imageList: res.data.list,
        buttonShow: true,
      });
    }
  },
  async deleteImage(e) {
    const res = await request({
      url:
        app.globalData.baseURL +
        `/app-user-api/member/qrcode/miniapp/delete?id=${e.target.dataset.index}`,
      method: "DELETE",
    });
    if (res.code == 0) {
      wx.showToast({
        title: "删除二维码成功",
        icon: "success",
      });
      this.getImage();
    }
  },
  gobigimage(e) {
    let arr = [];
    arr.push(e.currentTarget.dataset.parmas);
    wx.previewImage({
      urls: arr, //imgUrl 必须是需要预览的图片链接列表，只有一张图片也需要是列表
      success: (res) => {
        console.log("接口调用成功", res);
      },
    });
  },
  async createCode() {
    const name = wx.getStorageSync("username");
    const res = await request({
      url:
        app.globalData.baseURL + "/app-user-api/member/qrcode/miniapp/create",
      method: "POST",
      data: {
        name: name,
        path: "pages/index/index",
        px: 430,
        version: "release",
        alpha: false,
      },
    });
    if (res.code == 0) {
      wx.showToast({
        title: "生成二维码成功",
        icon: "success",
      });
      this.setData({
        name: "",
        page: "",
        size: "",
        buttonShow: false,
      });
      this.getImage();
    } else {
      wx.showToast({
        title: res.msg,
        icon: "none",
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    return {
      title: "嘉之勤官网小程序",
      path: "/pages/index/index",
      imageUrl:
        "https://youyougo2.oss-cn-chengdu.aliyuncs.com/0201506f6fc0f4145e34159c2e2ac66b94b96450dd29e5a19d177edc5dad8023.png",
    };
  },
});
